/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.util.StorageManager;
import storage.dbobject.DbLogBody;
/**
 * 
 * @author fenyo rome
 *
 */
public class LogBodyHelper {

	/**
	 * 将logBody保存到LaUDMS数据库中
	 * 
	 * @param logBody
	 *            要存储的模型对象
	 */
	public void saveLogBody(DbLogBody logBody) {
		// StorageManager sm = new StorageManager(StorageParameter.db_keySpace,
		// StorageParameter.db_columnFamily_logBody);
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logBody"));
		// sm.open();
		sm.setRowKey(logBody.getRowKey());
		sm.put(DbLogBody.CL_NAME, logBody.getName());
		sm.put(DbLogBody.CL_ABBREVIATION, logBody.getAbbreviation());
		sm.put(DbLogBody.CL_CHINESE, logBody.getChinese());
		sm.put(DbLogBody.CL_HASVALUE, logBody.getHasValue());
		Hashtable<String, String> valueDescriptionMap = logBody
				.getValueDescriptionMap();
		Enumeration valueEnume = valueDescriptionMap.keys();
		while (valueEnume.hasMoreElements()) {
			String valueTemp = (String) valueEnume.nextElement();
			sm.put(DbLogBody.CL_VALUE + "_" + valueTemp,
					valueDescriptionMap.get(valueTemp));
		}
		// sm.close();
	}

	/**
	 * 将logBody保存到LaUDMS数据库中
	 * 
	 * @param logBody
	 *            要存储的模型对象
	 */
	public void updateLogBody(DbLogBody logBody) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logBody"));
		// sm.open();
		sm.delete(logBody.getRowKey());
		sm.setRowKey(logBody.getRowKey());
		sm.put(DbLogBody.CL_NAME, logBody.getName());
		sm.put(DbLogBody.CL_ABBREVIATION, logBody.getAbbreviation());
		sm.put(DbLogBody.CL_CHINESE, logBody.getChinese());
		sm.put(DbLogBody.CL_HASVALUE, logBody.getHasValue());
		Hashtable<String, String> valueDescriptionMap = logBody
				.getValueDescriptionMap();
		Enumeration valueEnume = valueDescriptionMap.keys();
		while (valueEnume.hasMoreElements()) {
			String valueTemp = (String) valueEnume.nextElement();
			sm.put(DbLogBody.CL_VALUE + "_" + valueTemp,
					valueDescriptionMap.get(valueTemp));
		}
		// sm.close();
	}

	/**
	 * 将一系列xes保存到LaUDMS数据库中
	 * 
	 * @param logBodys
	 *            要存储的模型对象列表
	 */
	public void saveLogBodys(List<DbLogBody> logBodys) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logBody"));
		// sm.open();
		for (int i = 0; i < logBodys.size(); i++) {
			DbLogBody logBody = logBodys.get(i);
			sm.setRowKey(logBody.getRowKey());
			sm.put(DbLogBody.CL_NAME, logBody.getName());
			sm.put(DbLogBody.CL_ABBREVIATION, logBody.getAbbreviation());
			sm.put(DbLogBody.CL_CHINESE, logBody.getChinese());
			sm.put(DbLogBody.CL_HASVALUE, logBody.getHasValue());
			Hashtable<String, String> valueDescriptionMap = logBody
					.getValueDescriptionMap();
			Enumeration valueEnume = valueDescriptionMap.keys();
			while (valueEnume.hasMoreElements()) {
				String valueTemp = (String) valueEnume.nextElement();
				sm.put(DbLogBody.CL_VALUE + "_" + valueTemp,
						valueDescriptionMap.get(valueTemp));
			}

		}
		// sm.close();
	}
	/**
	 * 
	 * @param columnName
	 * columnName
	 * @param value
	 * value
	 * @return
	 */
	public List<DbLogBody> getLogBodys(final String columnName,
			final String value) {
		List<DbLogBody> result = new LinkedList<DbLogBody>();
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logBody"));
		// sm.open();
		List<KeySlice> crs = sm.get(columnName, value);
		try {
			for (KeySlice ks : crs) {
				String rowKey = new String(ks.getKey());
				String name = null, abbreviation = null, chinese = null, hasValue = null;
				Hashtable<String, String> valueDescriptionMap = new Hashtable<String, String>();
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());

					if (columnsName.equals(DbLogBody.CL_NAME)) {
						name = columnsValue;
					} else if (columnsName.equals(DbLogBody.CL_ABBREVIATION)) {
						abbreviation = columnsValue;
					} else if (columnsName.equals(DbLogBody.CL_CHINESE)) {
						chinese = columnsValue;
					} else if (columnsName.equals(DbLogBody.CL_HASVALUE)) {
						hasValue = columnsValue;
					} else if (columnsName.startsWith(DbLogBody.CL_VALUE)) {
						String valueTemp = columnsName
								.substring(DbLogBody.CL_VALUE.length()
										+ "_".length());
						valueDescriptionMap.put(valueTemp, columnsValue);
					}
				}
				DbLogBody pLogBody = new DbLogBody(rowKey, name, abbreviation,
						chinese, hasValue, valueDescriptionMap);
				result.add(pLogBody);
			}
		} catch (Exception ex) {
			Logger.getLogger(DbLogBody.class.getName()).log(Level.SEVERE, null,
					ex);
		}
		return result;
	}
/**
 * 
 * @param rowKey
 * rowKey
 */
	public void deleteLogBody(final String rowKey) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logBody"));
		// sm.open();
		sm.delete(rowKey);
		// sm.close();
	}
	/**
	 * 
	 * @return ArrayList<String>
	 */
	public ArrayList<String> getAllLogBodyName() {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logBody"));
		// sm.open();
		List<KeySlice> crs = sm.getAll();
		ArrayList<String> logBodyNameList = new ArrayList<String>();
		try {
			for (KeySlice ks : crs) {
				String name = null;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbLogBody.CL_NAME)) {
						name = columnsValue;
					}
				}
				if (!logBodyNameList.contains(name)) {
					logBodyNameList.add(name);
				}
			}
		} catch (Exception ex) {
			Logger.getLogger(LogBodyHelper.class.getName()).log(Level.SEVERE,
					null, ex);
		} 
		return logBodyNameList;
	}
	/**
	 * 
	 * @param logBody
	 * logBody
	 * @param value
	 * value
	 * @param description
	 * description
	 */
	public void addValueDesceiption(DbLogBody logBody, String value,
			String description) {
		List<DbLogBody> result = new LinkedList<DbLogBody>();
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_logBody"));
		// sm.open();
		sm.setRowKey(logBody.getRowKey());
		if (!logBody.getHasValue().equalsIgnoreCase("yes")) {
			sm.put(DbLogBody.CL_HASVALUE, "yes");
			logBody.setHasValue("yes");
		}
		sm.put(DbLogBody.CL_VALUE + "_" + value, description);
		// sm.put(DbLogBody.cl_description + "_" + value, description);
		// sm.close();
	}

}
